package models

import (
	"strconv"
	"time"
)

/*组件*/
type Component struct {
	Id               string          `orm:"pk"`
	RowId            string          `orm:"column(row_id)"`
	ProjectVersionId *ProjectVersion `orm:"column(project_version_id);rel(fk)"`
	Deleted          int32           `orm:"column(deleted)"`
	Name             string
	Type             string
	FolderId         *Folder `orm:"column(folder_id);rel(fk)"`
	Shared           int32

	InputModelId  *Model    `orm:"column(input_model_id);rel(fk)"`
	OutputModelId *Model    `orm:"column(output_model_id);rel(fk)"`
	ResourceId    *Resource `orm:"column(resource_id);rel(fk)"`

	CreateTime     time.Time `orm:"column(create_time);type(datetime)"`
	CreateBy       string    `orm:"column(create_by)"`
	LastUpdateBy   string    `orm:"column(last_update_by)"`
	LastUpdateTime time.Time `orm:"column(last_update_time);type(datetime)"`

	ComponentAttributeSettings []*ComponentAttributeSetting `orm:"reverse(many)"` // fk 的反向关系

	ComponentEntitySettings []*ComponentEntitySetting `orm:"reverse(many)"` // fk 的反向关系
	ComponentSettings       []*ComponentSetting       `orm:"reverse(many)"` // fk 的反向关系

	ComponentDefinition  map[string]interface{} `orm:"-"`
	Flows                []*Flow                `orm:"-"`
	DropDownModels       []*Model               `orm:"-"`
	ComponentSettingsMap map[string]string      `orm:"-"`
}

func (this *Component) GetBoolean(name string, defaultValue bool) bool {
	r := defaultValue

	if this.ComponentSettingsMap[name] == "true" {
		return true
	} else {
		if this.ComponentSettingsMap[name] == "false" {
			return false
		}
	}

	return r
}

func (this *Component) GetInt(name string, defaultValue int32) int32 {
	r := defaultValue

	if val, ok := this.ComponentSettingsMap[name]; ok {
		n, err := strconv.Atoi(val)
		if err == nil {
			r = int32(n)
		}
	}

	return r
}

/*组件属性设置*/
type ComponentAttributeSetting struct {
	Id          string          `orm:"pk"`
	ComponentId *Component      `orm:"column(component_id);rel(fk)"`
	AttributeId *ModelAttribute `orm:"column(attribute_id);rel(fk)"`
	Name        string
	Value       string `orm:"column(value);type(longtext)"`

	CreateTime     time.Time `orm:"column(create_time);type(datetime)"`
	CreateBy       string    `orm:"column(create_by)"`
	LastUpdateBy   string    `orm:"column(last_update_by)"`
	LastUpdateTime time.Time `orm:"column(last_update_time);type(datetime)"`
}

/*组件属性设置*/
type ComponentEntitySetting struct {
	Id          string       `orm:"pk"`
	ComponentId *Component   `orm:"column(component_id);rel(fk)"`
	EntityId    *ModelEntity `orm:"column(entity_id);rel(fk)"`
	Name        string
	Value       string `orm:"column(value);type(longtext)"`

	CreateTime     time.Time `orm:"column(create_time);type(datetime)"`
	CreateBy       string    `orm:"column(create_by)"`
	LastUpdateBy   string    `orm:"column(last_update_by)"`
	LastUpdateTime time.Time `orm:"column(last_update_time);type(datetime)"`
}

/*组件属性设置*/
type ComponentSetting struct {
	Id          string     `orm:"pk"`
	ComponentId *Component `orm:"column(component_id);rel(fk)"`
	Name        string
	Value       string `orm:"column(value);type(longtext)"`

	CreateTime     time.Time `orm:"column(create_time);type(datetime)"`
	CreateBy       string    `orm:"column(create_by)"`
	LastUpdateBy   string    `orm:"column(last_update_by)"`
	LastUpdateTime time.Time `orm:"column(last_update_time);type(datetime)"`
}
